<template>
	<view class="box">
		<uNavbar title="修改手机号码" bgColor="transparent"></uNavbar>
		<view class="title">
			当前手机号：{{user.phone}}
		</view>
		<view class="main_item">
			<view class="main_item_title">
				手机号
			</view>
			<input type="number" maxlength="11" class="ipt" :value="phone" placeholder="请输入新手机号" @input="getphone" />
		</view>
		<view class="binding flex_dq" style="margin-top: 30px;">
			<u-input  type="number" placeholder="请输入验证码" maxlength="6" v-model.trim="validateCode">
				<template slot="suffix">
					<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
					<u-button @tap="varify2()" :text="tips" type="success" size="mini"></u-button>
				</template>
			</u-input>
		</view>
		<view v-if="isyzm">
			<web-view :fullscreen="false"  src="/static/local.html" @message="callback2">测试</web-view>
		</view>
		<view class="btn" @click="submit">
			确定
		</view>
	</view>
</template>

<script>
	import {getuserinfo} from "../../api/index.js";
	import {editPhone} from "../../api/setting.js";
	import {
		sendSms
	} from "../../api/login.js";
	export default {
		data() {
			return {
				phone: "",
				validateCode: "",
				tips: '',
				user:{},
				canClick:true,
				isyzm:false,
				randstr:"",
				ticket:"",
			}
		},
		onLoad() {
			this.getuser()
		},
		computed: {
			isPhoneNumberValid() {
				const phoneNumberRegex = /^1[3-9]\d{9}$/;
				return phoneNumberRegex.test(this.phone);
			},

		},
		methods: {
			callback2(res){
				let that=this
				that.isyzm=false
				let r = res.detail.data[0].res;
				if (r.ret === 0) {
					that.randstr=r.randstr
					that.ticket=r.ticket
					that.getCode()
					console.log("成功")
					//成功，传递数据给后台进行验证
					// 这里后台会给一个借口,把后台需要的参数传过去
					
				} else {
					console.log("验证失败")
					// 提示用户完成验证
				}
				         console.log(1111111111,r);
			},
			callback(res) {
				// 用户操作验证码或者关闭刷新腾讯返回的内容 
				let that = this
				if (res.ret === 0) {
					that.randstr=res.randstr
					that.ticket=res.ticket
					that.getCode()
					console.log("成功")
					//成功，传递数据给后台进行验证
					// 这里后台会给一个借口,把后台需要的参数传过去
					
				} else {
					console.log("验证失败")
					// 提示用户完成验证
				}
			},
			varify2(){
				// #ifdef APP
					if (uni.$u.test.isEmpty(this.phone)) {
						uni.$u.toast('请输入您的手机号');
						
					}else{
						this.isyzm=true
					}
					
				// #endif
				// #ifdef H5
					if (uni.$u.test.isEmpty(this.phone)) {
						uni.$u.toast('请输入您的手机号');
						
					}else{
						this.varify()
					}
					
				// #endif
			},
			varify() {
				let appid = '197018098'; // 腾讯云控制台中对应这个项目的 appid
				//生成一个滑块验证码对象
				let captcha = new TencentCaptcha(appid, this.callback);
				// 滑块显示
				captcha.show();
			
			},
			getuser(){
				getuserinfo().then(res=>{
					this.user=res.data
					// console.log(this.user)
				})
			},
			getphone(e) {
				this.phone = e.detail.value
			},
			getidcard(e) {
				this.idcard = e.detail.value
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				// console.log(11111);
				let that = this;
				let data = {
					phone: that.user.phone,
					scene: 'edit_phone',
					randstr:that.randstr,
					ticket:that.ticket
				}
				sendSms(data).then(res => {
					console.log(res);
					if (res.code == 1) {
						// that.validateCode=res.data
						uni.$u.toast(res.msg);
						if (that.$refs.uCode.canGetCode) {
							that.$refs.uCode.start();
						}
					}
				})
			},
			submit(){
				if(!this.isPhoneNumberValid){
					uni.$u.toast('请输入正确手机号');
					return;
				}else{
							if (!this.canClick) {
					        return;
					      }
					      this.canClick = false;
					editPhone({
						phone:this.phone,
						sms_code:this.validateCode,
						
					}).then(res=>{
						if(res.code==1){
							setTimeout(()=>{
								uni.navigateBack()
							},1500)
							
						}
						uni.$u.toast(res.msg);
						 setTimeout(() => {
						        this.canClick = true;
						      }, 1000); // 设定一个点击间隔，防止频繁点击
						console.log()
					})
					
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		width: 750rpx;
		height: 100vh;
		background-color: #E0DDDA;
	}

	.title {
		width: 700rpx;
		margin-top: 40rpx;
		font-weight: 400;
		font-size: 32rpx;
		color: #333;
		margin-left: 50rpx;
	}

	.main_item {
		width: 750rpx;

		.main_item_title {
			font-weight: 400;
			font-size: 30rpx;
			color: #333;
			margin-left: 50rpx;
			margin-top: 70rpx;
		}

		.ipt {
			margin: 0 auto;
			margin-top: 40rpx;
			width: 638rpx;
			height: 50rpx;
			font-weight: 300;
			font-size: 30rpx;
			color: #fff  !important;
			border-bottom: 2rpx solid #CFC4B7;
		}
	}

	.binding {
		width: 90%;
		margin: 0 auto;
		padding: 18rpx 0;
		border-radius: 60rpx;
		// background: #2B2749;
		/* border-bottom: 1rpx solid #5a5a5a70; */
	}

	/deep/.u-button--success {
		background: none !important;
		border: none !important;
		color: #A68A64 !important;
	}
/deep/.uni-input-input{
	color: #333;
	font-weight: 300;
	font-size: 30rpx;
}
	/deep/.u-border {
		border: none;
		border-bottom: 2rpx solid #CFC4B7!important;
	}

	/deep/.uni-input-wrapper {
		color: #A6A5AF !important;
	}

	/deep/.uni-input-placeholder {
		color: #A6A5AF !important;
	}

	.btn {
		width: 690rpx;
		height: 88rpx;
	background: linear-gradient( 90deg, #AB8E68 0%, #D7BA93 100%);
		border-radius: 332rpx 332rpx 332rpx 332rpx;
		margin: 0 auto;
		margin-top: 50rpx;
		font-weight: 400;
		font-size: 36rpx;
		color: #FFFFFF;
		text-align: center;
		line-height: 88rpx;
	}
	// /deep/.uni-input-placeholder{
	// 	color: #A6A5AF !important;
	// }
</style>